
package com.example.testutil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testutil.util.APKutil;
import com.example.testutil.util.LogUtil;
import com.example.testutil.util.ZipUtil;
import com.example.testutil.util.contentprovider.Persons;
import com.example.testutil.util.file.JsonUtil;
import com.example.testutil.util.file.MD5Util;
import com.example.testutil.util.file.SAXparseXml;
import com.example.testutil.util.file.XMLparse;
import com.example.testutil.util.net.DownloadUtil;
import com.example.testutil.util.net.NetWorkUtil;
import com.example.testutil.util.sql.DbOperate;
import com.example.testutil.util.sql.MySqliteOpenHelper;
import com.example.testutil.util.sql.TableContants;

public class MainActivity extends Activity {

    public static final String TAG = "raymond";

    private TextView mView;

    String mUpdateUrl = "http://mobile.ac.qq.com/qqcomic_android.apk";

    String pathString = Environment.getExternalStorageDirectory() + "/raymond/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mView = (TextView) findViewById(R.id.tv_view);
        ViewConfiguration mConfiguration = ViewConfiguration.get(this);
        // testSQL();
        // testContentprovider();
        // testJson();
        // DownloadUtil.GetWithOkttp("https://www.baidu.com");
    }

    public void onClick(View v) {
        testZip();
        LogUtil.d("------------------------");
    }

    /*
     * 测试网络状态,获取 wifi 数据网络
     */
    public void testNetState() {
        LogUtil.d("net type--------" + NetWorkUtil.getNetworkType(this));
        LogUtil.d("wifi --------" + NetWorkUtil.isWiFiConnect(this));
        LogUtil.d("moblie --------" + NetWorkUtil.isMobileConnect(this));
        LogUtil.d("网络状态 --------" + NetWorkUtil.getAPNType(this));
    }

    /*
     * 利用httpconnection和AsyncTask组合下载
     */
    public void testHttpDownload() {
        DownloadUtil.getInstance().execute(mUpdateUrl, pathString, "qqcomic_android.apk", null);
    }

    /*
     * 测试MD5和进行apk 安装
     */
    public void testMd5() {
        File mFile = new File(pathString, "qqcomic_android.apk");
        LogUtil.d("raymond------------------" + MD5Util.md5(mFile));
        LogUtil.d("raymond1------------------" + MD5Util.calcMD5("raymond"));
        LogUtil.d("raymond1------------------" + MD5Util.calcMD5(mFile));
        String path = Environment.getExternalStorageDirectory() + "/raymond/qqcomic_android.apk";
        APKutil.installApk(this, path);
    }

    /*
     * create json data and parse json data
     */
    public void testJson() {
        String jsonString = JsonUtil.createJson().toString();
        LogUtil.d("-------------------" + jsonString);
        String parseString = JsonUtil.jsonParse(jsonString);
        LogUtil.d("----------parseString----" + parseString);
        JsonUtil.parseJsonWithGson(jsonString);
        Toast.makeText(this, jsonString, Toast.LENGTH_SHORT).show();
    }

    /*
     * parse XML with PULL DOM SAX;
     */
    public void testParseXml() {
        try {
            InputStream mInputStream = getAssets().open("subject.xml");
            XMLparse.parseWithDOM(mInputStream);
            XMLparse.XMLparseWithPULL(mInputStream);
            SAXParser mParser = SAXParserFactory.newInstance().newSAXParser();
            SAXparseXml mXml = new SAXparseXml();
            mParser.parse(mInputStream, mXml);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public void testSQL() {
        // 参考链接:
        ContentValues mValues = new ContentValues();
        mValues.put(TableContants.USER_NAME, "yangfan");
        mValues.put(TableContants.USER_NUM, 001);
        mValues.put(TableContants.USER_SEX, "man");
        long rawID = DbOperate.getInstance(this).insert(MySqliteOpenHelper.sqlname, null, mValues);
        LogUtil.d("rawId-------------------" + rawID);

        Cursor mCursor = DbOperate.getInstance(this).query(MySqliteOpenHelper.sqlname, null, null, null, null, null,
                null);
        if (mCursor.moveToFirst()) {
            do {
                String name = mCursor.getString(mCursor.getColumnIndex(TableContants.USER_NAME));
                int num = mCursor.getInt(mCursor.getColumnIndex(TableContants.USER_NUM));
                String sex = mCursor.getString(mCursor.getColumnIndex(TableContants.USER_SEX));
                LogUtil.d("name----" + name + "------" + num + "-----" + sex);
            } while (mCursor.moveToNext());
        }
        mCursor.close();
    }

    /*
     * 测试contentprovider
     */
    public void testContentprovider() {
        Uri mUri = Persons.Person.PERSON_URI;
        Cursor mCursor = this.getContentResolver().query(mUri, new String[] {
                "name", "sex", "number"
        }, null, null, null);
        if (mCursor != null) {
            if (mCursor.moveToFirst()) {
                do {
                    String name = mCursor.getString(mCursor.getColumnIndex(TableContants.USER_NAME));
                    int num = mCursor.getInt(mCursor.getColumnIndex(TableContants.USER_NUM));
                    String sex = mCursor.getString(mCursor.getColumnIndex(TableContants.USER_SEX));
                    LogUtil.d("name----" + name + "------" + num + "-----" + sex);
                } while (mCursor.moveToNext());
            }
        }
    }

    /*
     * 隐藏状态栏,实现全屏
     */
    public void SetFullscreenColor() {
        View mView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        mView.setSystemUiVisibility(option);
    }

    @SuppressLint("NewApi")
    public void setTransparentColor() {
        if (Build.VERSION.SDK_INT > 21) {
            View mView = getWindow().getDecorView();
            int options = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            mView.setSystemUiVisibility(options);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (Build.VERSION.SDK_INT > 19) {
            View mView = getWindow().getDecorView();
            mView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
    }

    private void testZip() {
        ZipUtil.zipfolder("/mnt/usb/sda1/test.txt", "/mnt/usb/sda1/test.zip");
        //ZipUtil.unzipFolder("/mnt/usb/sda1/test.zip", "/mnt/usb/sda1/test.txt");
    }
}
